<!--
 * @Description: 拖放
 * @Author: rendc
 * @Date: 2021-09-15 10:44:39
 * @LastEditors: rendc
 * @LastEditTime: 2021-09-15 11:23:56
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>拖放</title>
  <style>
    #div1,
    #div3,
    #div2 {
      border: 1px solid #aaa;
      width: 80px;
      height: 80px;
      margin: 10px;
      padding: 10px;
      float: left;
    }
  </style>
</head>

<body>
  <div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
    <img id="img" style="border: 1px solid #aaa;" draggable="true" ondragstart="drag(event)" height="62"
      src="./briup.png" alt="图片丢失">
  </div>
  <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
  <div id="div3"></div>
</body>
<script>
  function allowDrop(ev) {
    ev.preventDefault();
  }

  function drag(ev) {
    console.log(ev);
    ev.dataTransfer.setData("Text", ev.target.id);
  }

  function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("Text");
    ev.target.appendChild(document.getElementById(data));
  }
</script>

</html>